﻿using System.Globalization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using OilRigSumoWrestlers.Levels;
using OilRigSumoWrestlers.Scenes;
using RaptorEngine;
using RaptorEngine.Components;
using RaptorEngine.Components.Drawable;

namespace OilRigSumoWrestlers.Controllables.GameModes
{
    public class KnockoutMode:Controllable
    {
        private Entity _player1;
        private Entity _player2;
        private int _p1Score;
        private int _p2Score;
        private Movable _p1Move;
        private Movable _p2Move;

        private DrawableText _score;
        private int _maxScore;

        private float _minHeight; // Height at which you "die"

        private ALevel _level;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="player1"></param>
        /// <param name="player2"></param>
        /// <param name="minHeight">Height at which level ends</param>
        /// <param name="maxScore"></param>
        /// <param name="level">Level to be used</param>
        public KnockoutMode(Entity entity, Entity player1, Entity player2, float minHeight, int maxScore, ALevel level) : base(entity)
        {
            //For setting position and orientation
            _player1 = player1;
            _player2 = player2;

            _level = level;

            //For setting acceleration and velocity
            _p1Move = (Movable)_player1.GetComponent(ComponentType.Movable);
            _p2Move = (Movable)_player2.GetComponent(ComponentType.Movable);

            _minHeight = minHeight;
            _maxScore = maxScore;

            CreateScene();
        }

        private void CreateScene()
        {
            Entity.Position = new Vector3(
                GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.X + GameEngine.Instance.Render.GraphicsDevice.Viewport.TitleSafeArea.Width / 2,
                40,
                0);

            _score = new DrawableText(Entity, GameEngine.Instance.Load<SpriteFont>("Fonts/MenuFont"), "0 vs 0");
            _score.CenterText();

            Entity.AddComponent(_score);
        }


        public override void Update(GameTime gameTime)
        {
            //Check if player is below the floor
            if (_player1.Position.Y < _minHeight)
            {
                _p2Score++;
                PlayerScored();
            }
            else if (_player2.Position.Y < _minHeight)
            {
                _p1Score++;
                PlayerScored();
            }

            if (_p1Score >= _maxScore)
            {
                PlayerWon("Player 1");
            }
            else if (_p2Score >= _maxScore)
            {
                PlayerWon("Player 2");
            }

        }

        private void PlayerScored()
        {
            _player1.Position = _level.GetPlayer1StartPosition();
            _player2.Position = _level.GetPlayer2StartPosition();

            _p1Move.Acceleration = Vector3.Zero;
            _p1Move.Velocity = Vector3.Zero;

            _p2Move.Acceleration = Vector3.Zero;
            _p2Move.Velocity = Vector3.Zero;

            _player1.Rotation = Quaternion.Identity;
            _player2.Rotation = Quaternion.Identity;

            _score.TextString =
                _p1Score.ToString(CultureInfo.InvariantCulture) + " vs " +
                _p2Score.ToString(CultureInfo.InvariantCulture);

            if (_p1Score < _maxScore && _p2Score < _maxScore)
            {
                GameEngine.Instance.Scene.StopCurrentScene();
                GameEngine.Instance.Scene.PushScene(new CountdownScene());
                GameEngine.Instance.Scene.ShowCurrentScene();
                GameEngine.Instance.Scene.StartCurrentScene();
            }
        }

        private void PlayerWon(string playerName)
        {
            GameEngine.Instance.Scene.StopCurrentScene();
            GameEngine.Instance.Scene.PushScene(new WinScene(playerName));
            GameEngine.Instance.Scene.StartCurrentScene();
            GameEngine.Instance.Scene.ShowCurrentScene();
        }
    }
}
